package com.rongke.web.heepay;

import com.rongke.web.heepay.config.PartnerConfig;
import com.rongke.web.heepay.model.QuickPayRequest;
import com.rongke.web.heepay.util.Des.Des;
import com.rongke.web.heepay.util.HttpUtil;
import com.rongke.web.heepay.util.SmallTools;

/**
 * Created by Administrator on 2018/2/8.
 * 获取Token
 */
public class GetToken {
   // public static void main(String[] args){
       // getToken();
   // }//

    public static String getToken(QuickPayRequest quickPayRequest) {
        String url = "https://open.heepay.com/quickPay.do";

        //请求参数
        //*注：此demo中的参数仅供测试使用，切勿上线使用，上线产品请使用自己的账号
        String merchantId = PartnerConfig.merchantId;//商户号
        String merchantOrderNo = quickPayRequest.getMerchantOrderNo();//商户订单号
        String merchantUserId = quickPayRequest.getMerchantUserId();//商户下面的用户ID
        String payAmount = quickPayRequest.getPayAmount();//金额
        String requestTime = SmallTools.getDate("yyyyMMddHHmmss");//请求时间，yyyyMMddHHmmss
        String version = PartnerConfig.version;//版本
        String productCode = "CP06";//产品编码
        String hyAuthCode = quickPayRequest.getHyAuthCode();//授权码
        String bankCardInfo = quickPayRequest.getBankCardInfo();//银行卡信息 //"郭新胜^6230520320015163975^130323199407125632^18324465241"
        String description = quickPayRequest.getDescription();//产品信息
        String merchantUserName = quickPayRequest.getMerchantUserName();//用户名
        String requestUserIp = quickPayRequest.getRequestUserIp();//用户IP
        String notifyUrl = PartnerConfig.notifyUrl;//异步通知地址
        String sign = "";//签名
        String key = PartnerConfig.payKey;//商户密钥
        String deskey = key.substring(0,24);//商户密钥

        //3DES加密姓名和身份证号
        bankCardInfo = Des.Encrypt3Des(bankCardInfo,deskey);
        //拼接签名串
        String sign1 = "bankCardInfo="+bankCardInfo+
                "&description="+description+
                "&hyAuthCode="+hyAuthCode+
                "&merchantId="+merchantId+
                "&merchantOrderNo="+merchantOrderNo+
                "&merchantUserId="+merchantUserId+
                "&merchantUserName="+merchantUserName+
                "&notifyUrl="+notifyUrl+
                "&payAmount="+payAmount+
                "&productCode="+productCode+
                "&requestTime="+requestTime+
                "&requestUserIp="+requestUserIp+
                "&version="+version+
                "&key="+key;

        System.out.println("签名参数："+sign1);
        //对签名参数进行MD5加密得到sign
        sign = SmallTools.MD5en(sign1);
        //拼接请求参数
        String parameter = "merchantId="+merchantId+
                "&merchantOrderNo="+merchantOrderNo+
                "&merchantUserId="+merchantUserId+
                "&payAmount="+payAmount+
                "&requestTime="+requestTime+
                "&version="+version+
                "&productCode="+productCode+
                "&hyAuthCode="+hyAuthCode+
                "&bankCardInfo="+bankCardInfo+
                "&description="+description+
                "&merchantUserName="+merchantUserName+
                "&requestUserIp="+requestUserIp+
                "&notifyUrl="+notifyUrl+
                "&sign="+sign;
        System.out.println("请求参数："+parameter);
        String ret = HttpUtil.sendPost(url,parameter);
        System.out.println("返回的数据："+ret);
        Boolean isSign = SmallTools.checkSign(ret, key);
        System.out.println("验签结果："+isSign);
        if (isSign){
            return ret;
        }else {
            return "{\"retMsg\":\"返回数据验签失败\"}";
        }
    }
}
